from selenium import webdriver
import time
from scrapy.http.response.html import HtmlResponse
# 实现无可视化界面
from selenium.webdriver.chrome.options import Options
# 实现规避检测
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


class SeleniumMiddleware():
    def __init__(self):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        # 实现规避检测
        option = ChromeOptions()

        option.add_experimental_option('excludeSwitches', ['enable-automation'])
        prefs = {
            'profile.default_content_setting_values': {
                'images': 2,
                'permissions.default.stylesheet': 2
            }
        }
        option.add_experimental_option('prefs', prefs)
        self.browser = webdriver.Chrome(executable_path='D:\chromedriver\chromedriver-win64\chromedriver.exe', chrome_options=chrome_options,
                                    options=option)

    def __del__(self):
        self.browser.close()

    def process_request(self, request, spider):
        try:
            self.browser.get(request.url)
            time.sleep(2)
            source = self.browser.page_source


            response = HtmlResponse(url=self.browser.current_url, body=source, request=request,encoding='utf-8')
            return response
        except:
            return HtmlResponse(url=request.url, status=500, request=request)

